<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au" xmlns:int="http://schema.pbs.gov.au/DoHA-Internal" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:skos="http://www.w3.org/2004/02/skos/core#">

  <db:article>
    <db:info>
      <db:title>Extract continued dispensing file</db:title>

      <db:copyright>
        <db:year>2013</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").
          The following terms apply to all files associated with the software unless explicitly
          disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this
          software and its documentation for any purpose, provided that existing copyright notices
          are retained in all copies and that this notice is included verbatim in any distributions.
          No written agreement, license, or royalty fee is required for any of the authorized uses.
          Modifications to this software may be copyrighted by their authors and need not follow the
          licensing terms described here, provided that the new terms are clearly indicated on the
          first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
          DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
          THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE
          BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
          TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
          NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS
          NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
          MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>John-Paul Kelly</db:personname>
        <db:email>john.paul.kelly@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health</db:orgname>
          <db:orgdiv>PIMS</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>1.1.1</db:revnumber>
          <db:date>2013-11-06</db:date>
          <db:authorinitials>JPK</db:authorinitials>
          <db:revremark>Fix variable definition</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.1</db:revnumber>
          <db:date>2013-10-29</db:date>
          <db:authorinitials>JPK</db:authorinitials>
          <db:revremark>Map default values to 'Y' or 'N'</db:revremark>
        </db:revision>
      
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2013-06-21</db:date>
          <db:authorinitials>JPK</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet produces the continued dispensing extract file (<db:filename>cd_yyyymmdd.txt</db:filename>) from a PBS XML document.</db:para>
    <db:para>The source document must be a PBS XML document.</db:para>
    <db:para>The result document is a text file.</db:para>
    <db:para>The default field delimiter is the tab character.</db:para>
    <db:table>
      <db:tgroup cols="5" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="4" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Item Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>6</db:entry>
            <db:entry>Five or six alphanumeric characters in format (n)nnnna.</db:entry>
          </db:row>
          <db:row>
            <db:entry>LI Drug</db:entry>
            <db:entry>String</db:entry>
            <db:entry></db:entry>
            <db:entry>Optional: Legislative Instrument - drug name.  If this is not found, returns the MP name.</db:entry>
          </db:row>
          <db:row>
            <db:entry>LI Form</db:entry>
            <db:entry>String</db:entry>
            <db:entry></db:entry>
            <db:entry>Optional: Legislative Instrument - form and strength descriptor.  If this is not found, returns the MPP name.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Continued dispensing</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>1</db:entry>
            <db:entry>If 'Y', then Continued Dispensing is allowed.
            If 'N' then Continued Dispensing is not allowed.</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
  </db:article>

  <xsl:output method="text"/>

  <db:param>
    <db:refpurpose>Display</db:refpurpose>
    <db:para>Specify which fields to include and in which order</db:para>
  </db:param>
  <xsl:param name="display" select="&quot;item-code,cont-disp&quot;"/>

  <db:param>
    <db:refpurpose>Delimiter</db:refpurpose>
    <db:para>The character to use to separate fields.</db:para>
  </db:param>
  <xsl:param name="delimiter" select="&quot;&#9;&quot;"/>

  
  <xsl:key name="ident" match="*[not(ancestor::pbs:changes-list)]" use="@xml:id"/>
  <xsl:key name="concept" match="skos:Concept" use="@rdf:about"/>

  <db:template name="root">
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML Schema.</db:para>
  </db:template>
  <xsl:template match="/">
    <xsl:variable name="major" select="substring-before(/*/@version, &quot;.&quot;)"/>
    <xsl:choose>
      <xsl:when test="$major >= 2">
      <xsl:variable name="default" select="key(&quot;concept&quot;, &quot;pbs:continued-dispensing-default&quot;)/skos:note"/>
      <xsl:choose>
        <xsl:when test="key(&quot;concept&quot;, &quot;pbs:continued-dispensing-default&quot;)/skos:note">
          <xsl:call-template name="display">
            <xsl:with-param name="what" select="$display"/>
            <xsl:with-param name="item-code" select="&quot;V99F&quot;"/>
            <xsl:with-param name="LI-drug" select="&quot;*extemp-drug*&quot;"/>
            <xsl:with-param name="LI-form" select="&quot;*extemp-form*&quot;"/>
            <xsl:with-param name="cont-disp">
              <xsl:choose>
                <xsl:when test="$default = 'yes'">Y</xsl:when>
                <xsl:when test="$default = 'no'">N</xsl:when>
                <xsl:otherwise>M</xsl:otherwise>
              </xsl:choose>
            </xsl:with-param>
          </xsl:call-template>
          <xsl:text>&#xa;</xsl:text>
          <xsl:call-template name="display">
            <xsl:with-param name="what" select="$display"/>
            <xsl:with-param name="item-code" select="&quot;0001A&quot;"/>
            <xsl:with-param name="LI-drug" select="&quot;*extemp-drug*&quot;"/>
            <xsl:with-param name="LI-form" select="&quot;*extemp-form*&quot;"/>
            <xsl:with-param name="cont-disp">
              <xsl:choose>
                <xsl:when test="$default = 'yes'">Y</xsl:when>
                <xsl:when test="$default = 'no'">N</xsl:when>
                <xsl:otherwise>M</xsl:otherwise>
              </xsl:choose>
            </xsl:with-param>
          </xsl:call-template>
          <xsl:text>&#xa;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="display">
            <xsl:with-param name="what" select="$display"/>
            <xsl:with-param name="item-code" select="&quot;V99F&quot;"/>
            <xsl:with-param name="LI-drug" select="&quot;*extemp-drug*&quot;"/>
            <xsl:with-param name="LI-form" select="&quot;*extemp-form*&quot;"/>
            <xsl:with-param name="cont-disp" select="&quot;N&quot;"/>
          </xsl:call-template>
          <xsl:text>&#xa;</xsl:text>
          <xsl:call-template name="display">
            <xsl:with-param name="what" select="$display"/>
            <xsl:with-param name="item-code" select="&quot;0001A&quot;"/>
            <xsl:with-param name="LI-drug" select="&quot;*extemp-drug*&quot;"/>
            <xsl:with-param name="LI-form" select="&quot;*extemp-form*&quot;"/>
            <xsl:with-param name="cont-disp" select="&quot;N&quot;"/>
          </xsl:call-template>
          <xsl:text>&#xa;</xsl:text>
          <xsl:message terminate="no">Warning - default value for pbs:continued-dispensing not found</xsl:message>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates
        select="/pbs:root/pbs:schedule/pbs:listings-list/pbs:prescribing-rule[not(pbs:drug-tariff) and not(pbs:solvent-rule)] |
        /pbs:root/pbs:schedule/pbs:listings-list/pbs:prescribing-rule/pbs:prescribing-rule">
        <xsl:sort select="substring(pbs:code, 1, string-length(pbs:code) - 1)" data-type="number"
        />
      </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">This stylesheet is designed to operate upon a Schedule XML
          document that conforms to version 2.0 (or later) of the PBS XML Schema. See
          http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of
          this XSL stylesheet that operates on documents that conform to an earlier version of the
          schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="pbs:prescribing-rule">
    <!-- only process the first prescribing rule for the item -->
    <xsl:variable name="this" select="pbs:code"/>
    <xsl:if test="not(preceding-sibling::pbs:prescribing-rule[1][pbs:code = $this])">
      <xsl:variable name="item-code"><xsl:apply-templates select="pbs:code"/></xsl:variable>

      <xsl:variable name="LI-drug">
      <xsl:if test="contains($display,'LI-drug')">
        <xsl:choose>
          <xsl:when test="pbs:ready-prepared">
            <xsl:choose>
              <xsl:when test="pbs:ready-prepared/pbs:mpp-reference/int:pharmaceutical-item-reference">
                <xsl:value-of select="key('ident', substring(pbs:ready-prepared/pbs:mpp-reference/int:pharmaceutical-item-reference/@xlink:href, 2))/db:title"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="key('ident', substring(pbs:ready-prepared/pbs:mpp-reference/@xlink:href, 2))/../db:title"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>

          <xsl:when test="pbs:infusible">
            <xsl:choose>
              <xsl:when test="pbs:infusible/pbs:mpp-reference/int:pharmaceutical-item-reference">
                <xsl:value-of select="key('ident', substring(pbs:infusible/pbs:mpp-reference[1]/int:pharmaceutical-item-reference/@xlink:href, 2))/db:title"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="key('ident', substring(pbs:infusible/pbs:mp-reference/@xlink:href, 2))/db:title"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>

          <xsl:when test="pbs:extemporaneous-preparation">
            <xsl:text>*extemp-drug*</xsl:text>
          </xsl:when>

        </xsl:choose>
      </xsl:if>
      </xsl:variable>

      <xsl:variable name="LI-form">
      <xsl:if test="contains($display,'LI-form')">
        <xsl:choose>
          <xsl:when test="pbs:ready-prepared">
            <xsl:choose>
              <xsl:when test="pbs:ready-prepared/pbs:mpp-reference/int:pharmaceutical-item-reference">
                <xsl:value-of select="key('ident', substring(pbs:ready-prepared/pbs:mpp-reference/int:pharmaceutical-item-reference/@xlink:href, 2))/db:subtitle"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="key('ident', substring(pbs:ready-prepared/pbs:mpp-reference/@xlink:href, 2))/db:title"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>

          <xsl:when test="pbs:infusible">
            <xsl:text>*EFC-form*</xsl:text>
          </xsl:when>

          <xsl:when test="pbs:extemporaneous-preparation">
            <xsl:text>*extemp-form*</xsl:text>
          </xsl:when>

        </xsl:choose>
      </xsl:if>
      </xsl:variable>

      <xsl:variable name="cont-disp">
        <xsl:choose>
          <xsl:when test="pbs:continued-dispensing">Y</xsl:when>
          <xsl:otherwise>N</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:call-template name="display">
        <xsl:with-param name="what" select="$display"/>
        <xsl:with-param name="item-code" select="$item-code"/>
        <xsl:with-param name="LI-drug" select="$LI-drug"/>
        <xsl:with-param name="LI-form" select="$LI-form"/>
        <xsl:with-param name="cont-disp" select="$cont-disp"/>
      </xsl:call-template>
      <xsl:text>&#xa;</xsl:text>
    </xsl:if>
  </xsl:template>


  <!-- Codes are left-padded with zeroes -->
  <xsl:template match="pbs:code">
    <xsl:variable name="check" select="substring(., string-length(.))"/>
    <xsl:variable name="number" select="substring(., 1, string-length(.) - 1)"/>
    <xsl:value-of select="format-number($number, &quot;0000&quot;)"/>
    <xsl:value-of select="$check"/>
  </xsl:template>


  <db:template name="display">
    <db:refpurpose>Display values</db:refpurpose>
    <db:para>This template implements a generalised mechanism for displaying values from the data.
      The <db:literal>what</db:literal> parameter controls what values are included and in what
      order.</db:para>
  </db:template>
  <xsl:template name="display">
    <xsl:param name="what"/>
    <xsl:param name="item-code"/>
    <xsl:param name="LI-drug"/>
    <xsl:param name="LI-form"/>
    <xsl:param name="cont-disp"/>
    <xsl:choose>
      <xsl:when test="contains($what, ',')">
        <xsl:variable name="item" select="normalize-space(substring-before($what, ','))"/>
        <xsl:choose>
          <xsl:when test="$item = 'item-code'"><xsl:value-of select="$item-code"/></xsl:when>
          <xsl:when test="$item = 'LI-drug'"><xsl:value-of select="$LI-drug"/></xsl:when>
          <xsl:when test="$item = 'LI-form'"><xsl:value-of select="$LI-form"/></xsl:when>
          <xsl:when test="$item = 'cont-disp'"><xsl:value-of select="$cont-disp"/></xsl:when>
          <xsl:otherwise>unknown-variable</xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="$delimiter"/>
        <xsl:call-template name="display">
          <xsl:with-param name="what" select="substring-after($what, ',')"/>
          <xsl:with-param name="item-code" select="$item-code"/>
          <xsl:with-param name="LI-drug" select="$LI-drug"/>
          <xsl:with-param name="LI-form" select="$LI-form"/>
          <xsl:with-param name="cont-disp" select="$cont-disp"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$what = 'item-code'"><xsl:value-of select="$item-code"/></xsl:when>
          <xsl:when test="$what = 'LI-drug'"><xsl:value-of select="$LI-drug"/></xsl:when>
          <xsl:when test="$what = 'LI-form'"><xsl:value-of select="$LI-form"/></xsl:when>
          <xsl:when test="$what = 'cont-disp'"><xsl:value-of select="$cont-disp"/></xsl:when>
          <xsl:otherwise>unknown-variable</xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="text()"/>

</xsl:stylesheet>
